import md5 from './Md5';
// let URL = 'https://wxgmppl.sihai-inc.com/';
// let URL = 'https://wxgmzuma.sihai-inc.com/';
let key = '!@#$%(12345)';

let HLog = 1;

function HttpLog(msg) {
    if (HLog) {
        console.log(msg);
    }
}

let formatData = function (data) {

    let arr = [];
    for (let k in data) {
        let kv = `${k}${data[k]}&`;
        arr.push(kv);
    }
    arr.sort();
    let kvstr = key;
    arr.forEach(element => {
        kvstr = kvstr + element;
    });
    data.sign = md5.hex_md5(kvstr);

    //--------------------
    let str = '?';
    for (let k in data) {
        if (str !== '?') {
            str += '&';
        }
        str += k + '=' + data[k];
    }
    return str;
};


let HTTP = {

    sessionId: 0,
    userId: 0,
    master_url: null,
    url: null,

    configURL(url){
        this.master_url = this.url = url;
    },

    Get: function (path, data, handler) {
        let xhr = new XMLHttpRequest();
        // cc.loader.getXMLHttpRequest();
        xhr.timeout = 5000;
        let str = formatData(data);
        let requestURL = HTTP.url + path + encodeURI(str);
        HttpLog('HTTP 请求 =  ' + requestURL);
        xhr.open('GET', requestURL, true);
        if (cc.sys.isNative) {
            xhr.setRequestHeader('Accept-Encoding', 'gzip,deflate', 'text/html;charset=UTF-8');  // eslint-disable-line
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    HttpLog('Http 返回 ： ' + xhr.responseText);
                    try {
                        let ret = JSON.parse(xhr.responseText);
                        if (handler) {
                            handler(ret);
                        }
                    } catch (e) {
                        HttpLog('err:' + e);
                        HttpLog('err:' + requestURL);
                    }
                } else {
                    cc.log('xhr.status:', xhr.status);
                    HttpLog('XMLHttpRequest Error');
                    let result = {};
                    result.errcode = -2;
                    result.errmsg = 'XMLHttpRequest Error';
                    handler(result);
                }
            }
        };

        xhr.send();
        return xhr;
    },

    GetWithHeader: function (path, data, handler) {
        let xhr = new XMLHttpRequest();
        xhr.timeout = 5000;
        let requestURL = HTTP.url + path;
        HttpLog('HTTP 请求 =  ' + requestURL);
        xhr.open('GET', requestURL, true);
        for (let k in data) {
            xhr.setRequestHeader(k + '', data[k] + '');
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    HttpLog('Http 返回 ： ' + xhr.responseText);
                    try {
                        let ret = JSON.parse(xhr.responseText);
                        if (handler) {
                            handler(ret);
                        }
                    } catch (e) {
                        HttpLog('err:' + e);
                        HttpLog('err:' + requestURL);
                    }
                } else {
                    HttpLog('XMLHttpRequest Error');
                    let result = {};
                    result.errcode = -2;
                    result.errmsg = 'XMLHttpRequest Error';
                    handler(result);
                }
            }

        };
        xhr.send();
        return xhr;
    },

    Request: function (requestURL, handler) {
        let xhr = new XMLHttpRequest();
        xhr.timeout = 5000;
        HttpLog('HTTP 请求 =  ' + requestURL);
        xhr.open('GET', requestURL, true);
        if (cc.sys.isNative) {
            xhr.setRequestHeader('Accept-Encoding', 'gzip,deflate', 'text/html;charset=UTF-8');
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    HttpLog('Http 返回 ： ' + xhr.responseText);
                    try {
                        let ret = JSON.parse(xhr.responseText);
                        if (handler) {
                            handler(ret);
                        }
                    } catch (e) {
                        HttpLog('err:' + e);
                    }
                } else {
                    HttpLog('XMLHttpRequest Error');
                    let result = {};
                    result.errcode = -2;
                    result.errmsg = 'XMLHttpRequest Error';
                    handler(result);
                }
            }
        };

        xhr.send();
        return xhr;
    },

};

export default HTTP;
